var util = require('../../utils/util.js')
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    lists: [],
    shortName: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.datalist()
  },
  //记录列表查询//获取扫描记录API，默认获取十条记录，可以加count参数指定个数，offset是偏移量，
  
  datalist: function() {
    wx.showToast({
      title: '加载中..',
      icon: 'loading',
      //duration: 1500
    });
    var that = this
    wx.request({
      url: app.globalData.the_host + '/fcode/scanrecord?openId=' + app.globalData.openId + '&offset=0&count=10',

      header: {
        'content-type': 'application/json'
      },
      method: "get",
      success: res => {
        if (res.statusCode == 200) {
          this.bianli(res.data)
          // that.setData({
          //   lists: res.data,
          // })
         
        }

      }

    })
  },
  //处理一下数据
  //处理一下数据
  bianli: function (datalist) {
    var that = this;
    var list1 = []
    for (let i = 0; i < datalist.length; i++) {
      list1.push({
        id: datalist[i].id,
        fcode: datalist[i].fcode,
        vcode: datalist[i].vcode,
        province: datalist[i].province,
        city: datalist[i].city,
        address: datalist[i].address,
        productId: datalist[i].productId,
        productName: datalist[i].productName,
        xuliehao: datalist[i].fcode.substring(12, 20),
        modifiedTime: datalist[i].modifiedTime,
      });
    }
    that.setData({
      lists: list1,
    })
  },
  bianli1: function(datalist) {
    var that = this;
    var list1 = []
    for (let i = 0; i < datalist.length; i++) {
      var name = '00'
      that.getfcodedetect(datalist[i].fcode, function(result) {
        that.setData({
          shortName: result,
        })
        list1.push({
          id: datalist[i].id,
          fcode: datalist[i].fcode,
          vcode: datalist[i].vcode,
          province: datalist[i].province,
          city: datalist[i].city,
          address: datalist[i].address,
          productId: datalist[i].productId,
          name: that.data.shortName,
          xuliehao: datalist[i].fcode.substring(12, 20),
          modifiedTime: datalist[i].modifiedTime,
        });
        that.setData({
          lists: list1,
        })
      });
    }
  },
  //数据查询
  //p1是正常的参数,ajax_callback是回调函数(可以带回调参数)
  //调用的时候第二个参数传入一个不具名函数即可.

  getfcodedetect: function(p1, ajax_callback) {
    var that = this;
    wx.request({
      url: app.globalData.the_host + `/fcode/productbyfcode`,
      data: {
        "code": p1,
      },
      header: {
        'content-type': 'application/json'
      },
      method: "get",
      success: res => {
        ajax_callback(res.data.shortName);
        wx.hideToast(); //隐藏提示框
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.datalist()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})